package org.easymis.oa.modules.main.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.easymis.commons.web.page.SimplePage;
import org.easymis.commons.web.springmvc.JsonApi;
import org.easymis.commons.web.springmvc.JsonMessage;
import org.easymis.entitys.mybatis.dto.Member;
import org.easymis.entitys.mybatis.dto.OaDiary;
import org.easymis.entitys.mybatis.dto.OrganizeFunction;
import org.easymis.oa.service.datacenter.OrganizeFunctionMng;
import org.easymis.oa.service.main.OaDiaryMng;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;



/**
 * 
 * @See 个人日志每日日志
 *
 */
@Controller
@RequestMapping("/diary/day/")
public class OaDiaryDayAction {
	@Resource
	private OaDiaryMng service;
	@Autowired
	OrganizeFunctionMng organizeFunctionMng;
	@RequestMapping(value = "index")
	public String index(HttpServletRequest request, HttpSession session,ModelMap model) throws Exception {
		String functionId=request.getParameter("menuId");
		List<OrganizeFunction> breadcrumbList= organizeFunctionMng.getBreadcrumb(functionId);
		request.setAttribute("base", "http://demo.easymis.cn/oa");
		model.put("breadcrumbList", breadcrumbList);
		return "/diary/day/index";
	}
	@RequestMapping(value = "mine")
	public String forwardMine(HttpServletRequest request, HttpSession session,ModelMap model) throws Exception {
		String functionId=request.getParameter("menuId");
		List<OrganizeFunction> breadcrumbList= organizeFunctionMng.getBreadcrumb(functionId);
		request.setAttribute("base", "http://demo.easymis.cn/oa");
		model.put("breadcrumbList", breadcrumbList);
		return "/diary/day/mine";
	}
	@RequestMapping(value = "mine/ajax/grid")
	@ResponseBody
	public Object forwardMineList(Integer page, Integer pageSize,  HttpServletRequest request, HttpSession session) throws Exception {
		Member member =(Member)session.getAttribute("userInfo");
		JsonApi jsonApi = new JsonApi(false);
		if (member == null) {
			jsonApi = new JsonApi(2, JsonMessage.LOGINFAILED);
		} else {
			OaDiary params = new OaDiary();
			params.setOrgId(member.getOrgId());
			params.setStaffId(member.getStaffId());
			PageHelper.startPage(SimplePage.cpn(page), SimplePage.cps(pageSize));
			List<OaDiary> beanList = service.getList(params);
			PageInfo<OaDiary> p = new PageInfo<OaDiary>(beanList);
			jsonApi = new JsonApi(1,p);
		}
		return jsonApi;
	}
	@RequestMapping(value = "people")
	public String forwardPeople(HttpServletRequest request, HttpSession session,ModelMap model) throws Exception {
		String functionId=request.getParameter("menuId");
		List<OrganizeFunction> breadcrumbList= organizeFunctionMng.getBreadcrumb(functionId);
		request.setAttribute("base", "http://demo.easymis.cn/oa");
		model.put("breadcrumbList", breadcrumbList);
		return "/diary/day/people";
	}
	@RequestMapping(value = "myQuery")
	public String forwardIndex(HttpServletRequest request, HttpSession session,ModelMap model) throws Exception {
		String functionId=request.getParameter("menuId");
		List<OrganizeFunction> breadcrumbList= organizeFunctionMng.getBreadcrumb(functionId);
		request.setAttribute("base", "http://demo.easymis.cn/oa");
		model.put("breadcrumbList", breadcrumbList);
		return "/diary/day/myQuery";
	}

	@RequestMapping(value = "myQuery/grid", method = RequestMethod.POST)
	public String forwardList(Integer page, Integer pageSize,  HttpServletRequest request) throws Exception {
		OaDiary bean = new OaDiary();
		//bean.setOrgId(orgId);
		PageHelper.startPage(SimplePage.cpn(page), SimplePage.cps(pageSize));
		List diaryList=service.getList(bean);

		PageInfo<OaDiary> p = new PageInfo<OaDiary>(diaryList);
		request.setAttribute("base", "http://demo.easymis.cn/oa");		
		request.setAttribute("diaryList", diaryList);
		request.setAttribute("pageTotalResult", p.getTotal());
		return "/diary/day/myQuery_grid";
	}
	@RequestMapping(value = "review/query")
	public String forwardReviewQuery(HttpServletRequest request, HttpSession session,ModelMap model) throws Exception {
		String functionId=request.getParameter("menuId");
		List<OrganizeFunction> breadcrumbList= organizeFunctionMng.getBreadcrumb(functionId);
		request.setAttribute("base", "http://demo.easymis.cn/oa");
		model.put("breadcrumbList", breadcrumbList);
		return "/diary/day/review_query";
	}

	@RequestMapping(value = "review/grid", method = RequestMethod.POST)
	public String forwardReviewGrid(Integer page, Integer pageSize,  HttpServletRequest request,HttpSession session) throws Exception {
		Member userInfo =(Member)session.getAttribute("userInfo");
		OaDiary bean = new OaDiary();
		bean.setOrgId(userInfo.getOrgId());
		bean.setReviewerId(userInfo.getStaffId());
		PageHelper.startPage(SimplePage.cpn(page), SimplePage.cps(pageSize));
		List<OaDiary> diaryList=service.getList(bean);

		PageInfo<OaDiary> p = new PageInfo<OaDiary>(diaryList);
		request.setAttribute("base", "http://demo.easymis.cn/oa");		
		request.setAttribute("diaryList", diaryList);
		request.setAttribute("pageTotalResult", p.getTotal());
		return "/diary/day/review_query_grid";
	}

	@RequestMapping(value = "forwordEdit")
	public String forwardEdit(HttpServletRequest request, HttpSession session) throws Exception {
		Member userInfo =(Member)session.getAttribute("userInfo");

		
		request.setAttribute("base", "http://demo.easymis.cn/oa");
		
		return "/diary/day/edit";
	}
	@RequestMapping(value = "submitForReview", method = RequestMethod.POST)
	@ResponseStatus(value = HttpStatus.OK)
	public void submitForReview(HttpServletRequest request, HttpSession session) throws Exception {
		String id = request.getParameter("id");
		OaDiary bean = service.findById(id);
		bean.setStatus(2);
		service.update(bean);
	}
	@RequestMapping(value = "review/forwordEdit")
	public String forwardReviewEdit(HttpServletRequest request, HttpSession session) throws Exception {
		Member userInfo =(Member)session.getAttribute("userInfo");
		String id = request.getParameter("id");
		OaDiary bean = service.findById(id);
		
		request.setAttribute("diary", bean);
		request.setAttribute("base", "http://demo.easymis.cn/oa");
		
		return "/diary/day/review_edit";
	}
	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseStatus(value = HttpStatus.OK)
	@DateTimeFormat(pattern="yyyy-MM-dd HH:mm")
	public void save(HttpServletRequest request, HttpSession session, OaDiary bean) throws Exception {
		Member userInfo =(Member)session.getAttribute("userInfo");

		bean.setOrgId(userInfo.getOrgId());
		bean.setStaffId(userInfo.getStaffId());
		bean.setType(1);
		bean.setStatus(1);
		bean.setReviewWorkLevel(0);
		
		service.save(bean);

	}
	@RequestMapping(value = "review/save", method = RequestMethod.POST)
	@ResponseStatus(value = HttpStatus.OK)
	public void updatReview(HttpServletRequest request, HttpSession session) throws Exception {
		String id = request.getParameter("diaryId");
		OaDiary bean = service.findById(id);
		String reviewWorkLevel=request.getParameter("reviewWorkLevel");
		String reviewContent=request.getParameter("reviewContent");
		bean.setReviewWorkLevel(new Integer(reviewWorkLevel));
		bean.setReviewContent(reviewContent);
		bean.setReviewDate(new Date());		
		bean.setStatus(3);
		service.update(bean);
	}
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseStatus(value = HttpStatus.OK)
	public void delete(HttpServletRequest request, String id) throws Exception {		
		service.delete(id);
	}
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseStatus(value = HttpStatus.OK)
	public void updateaddressarea(HttpServletRequest request, HttpSession session) throws Exception {
		String id = request.getParameter("id");
		OaDiary bean = service.findById(id);
		service.update(bean);
	}
	@RequestMapping(value = "deleteByIds", method = RequestMethod.POST)
	@ResponseStatus(value = HttpStatus.OK)
	@ResponseBody
	public String deleteByIds(HttpServletRequest request) throws Exception {
		String ids_str = request.getParameter("id");
		String[] ids = ids_str.split(",");
		//service.deleteByIds(ids);
		
		return "";

	}
}
